package com.shaozi.nc.web.biz;

import java.util.Calendar;

import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.shaozi.lib.core.ActionRetBean;
import com.shaozi.nc.data.model.PushMsgModel;
import com.shaozi.nc.manage.PushMsgManage;
import com.shaozi.nc.web.beans.AddPushMsgReqBean;
import com.shaozi.nc.web.biz.base.BaseBizImpl;

@Service("bizPushMsg")
public class PushMsgBiz extends BaseBizImpl {

	@Autowired
	private PushMsgManage pushMgr;

	public PushMsgBiz() {

	}

	public void addPushMsg(ActionRetBean retBean, String compId, String userIds, String sourceId, long atTime,
			int sourceType, Long expireTime, Integer repeatCount, Integer intervalTimeSec, boolean isBadge,
			String title, String content, Integer subPushIntervalSec, Integer subPushRepeatCount) {
		if (atTime > 0) {
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(atTime);
			String pushSec = "0";
			String pushMin = String.valueOf(cal.get(Calendar.MINUTE));
			String pushHour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
			String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
			String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
			String week = "?";
			String year = String.valueOf(cal.get(Calendar.YEAR));
			AddPushMsgReqBean reqBean = new AddPushMsgReqBean();
			reqBean.setCompanyId(compId);
			reqBean.setUserId(userIds);
			reqBean.setSourceId(sourceId);
			reqBean.setPushSec(pushSec);
			reqBean.setPushMin(pushMin);
			reqBean.setPushHour(pushHour);
			reqBean.setDay(day);
			reqBean.setMonth(month);
			reqBean.setWeek(week);
			reqBean.setYear(year);
			reqBean.setSourceType(sourceType);
			reqBean.setExpireTime(expireTime);
			reqBean.setRepeatCount(repeatCount);
			reqBean.setIntervalTimeSec(intervalTimeSec);
			reqBean.setBadge(isBadge);
			reqBean.setTitle(title);
			reqBean.setContent(content);
			reqBean.setSubPushIntervalSec(subPushIntervalSec);
			reqBean.setSubPushRepeatCount(subPushRepeatCount);
			addPushMsg(retBean, reqBean);
		} else if (atTime == -1) {
			pushMgr.addPushMsgNow(compId, userIds, sourceId, sourceType, expireTime, repeatCount, intervalTimeSec,
					isBadge, title, content, subPushIntervalSec, subPushRepeatCount);
			retBean.setCode(0);
		}
	}

	/**
	 * 
	 * @param compId
	 * @param sourceId
	 * 
	 * @param pushSec
	 *            秒
	 * @param pushMin
	 *            分
	 * @param pushHour
	 *            时
	 * @param day
	 * @param month
	 * @param week
	 * @param year
	 * @param sourceType
	 *            1: im, 2:oa
	 * @param expireTime
	 * @param repeatCount
	 * @param intervalTimeSec
	 * @param isBadge
	 * @param title
	 * @param content
	 * @param subPushIntervalSec
	 * @param subPushRepeatCount
	 * @return
	 */
	public void addPushMsg(ActionRetBean retBean, AddPushMsgReqBean reqBean) {

		StringBuffer pushTimeRegex = new StringBuffer();
		pushTimeRegex.append(reqBean.getPushSec() == null ? "0" : reqBean.getPushSec()).append(" ");
		pushTimeRegex.append(reqBean.getPushMin() == null ? "0" : reqBean.getPushMin()).append(" ");
		pushTimeRegex.append(reqBean.getPushHour() == null ? "0" : reqBean.getPushHour()).append(" ");
		pushTimeRegex.append(reqBean.getDay() == null ? "*" : reqBean.getDay()).append(" ");
		pushTimeRegex.append(reqBean.getMonth() == null ? "*" : reqBean.getMonth()).append(" ");
		pushTimeRegex.append(reqBean.getWeek() == null ? "?" : reqBean.getWeek());
		if (!StringUtils.isEmpty(reqBean.getYear()))
			pushTimeRegex.append(" ").append(reqBean.getYear());
		String express = pushTimeRegex.toString();

		if (!CronExpression.isValidExpression(express)) {
			retBean.setCode(-1);
			return;
		}
		pushMgr.addPushMsg(reqBean.getCompanyId(), reqBean.getUserId(), reqBean.getSourceId(), express,
				reqBean.getSourceType(), reqBean.getExpireTime(), reqBean.getRepeatCount(),
				reqBean.getIntervalTimeSec(), reqBean.isBadge(), reqBean.getTitle(), reqBean.getContent(),
				reqBean.getSubPushIntervalSec(), reqBean.getSubPushRepeatCount());
		retBean.setCode(0);
	}

	public void delPushMsg(ActionRetBean retBean, String companyId, String userId, String sourceId, int sourceType) {
		PushMsgModel model = pushMgr.getMsgModel(companyId, userId, sourceId, sourceType);
		if (model == null) {
			retBean.setCode(-1);
			return;
		}
		pushMgr.remove(companyId, sourceType, model.getId());
		retBean.setCode(0);
	}
}
